package com.cd.javacxy.module.web.controller;

import com.cd.javacxy.common.model.entity.CxyTopic;
import com.cd.javacxy.common.model.enums.BlogStatusEnum;
import com.cd.javacxy.common.model.query.AskQuery;
import com.cd.javacxy.common.model.query.BlogQuery;
import com.cd.javacxy.common.model.query.KnowledgeQuery;
import com.cd.javacxy.common.model.query.TopicQuery;
import com.cd.javacxy.common.model.vo.SignInfoVo;
import com.cd.javacxy.common.utils.PageResult;
import com.cd.javacxy.module.base.BaseController;
import com.cd.javacxy.module.web.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * @author yanghy
 * @date 2019/11/19
 */
@Controller
public class IndexController extends BaseController {

    @Autowired
    private CxySignInService signInService;

    @Autowired
    private CxyTopicService topicService;

    @Autowired
    private CxyKnowledgeService knowledgeService;

    @Autowired
    private CxyAskService askService;

    @Autowired
    private CxyBlogService blogService;

    @RequestMapping("/")
    public ModelAndView index(HttpSession session) {
        Integer userId = this.getUserId(session);
        ModelAndView view = new ModelAndView("index");
        if (userId != null) {
            SignInfoVo signInfo = this.signInService.findSignInfoByUserId(userId);
            view.addObject("signInfo", signInfo);
        }
        PageResult<CxyTopic> topics = this.topicService.findTopicByPage(new TopicQuery());
        view.addObject("topics", topics);
        view.addObject("knowledges", this.knowledgeService.findKnowledgeByPage(new KnowledgeQuery()));
        view.addObject("asks", this.askService.findAskByPage(new AskQuery()));
        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setStatus(BlogStatusEnum.PUBLIC);
        view.addObject("blogs", this.blogService.findBlogByPage(blogQuery));
        return view;
    }
}
